package com.example.employee.controller;


import com.example.employee.entity.EmployeeDO;
import com.example.employee.service.EmployeeService;
import com.example.employee.util.ExcelUtils;
import com.example.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tc
 * @since 2022-07-20
 */
@Controller

public class EmployeeController  {
    @Autowired
    EmployeeService employeeService;

    @GetMapping("/")
    public String home(){

        return "dashboard";
    }
    @Resource
    private RedisUtils redisUtils;
    @GetMapping("/emps")
    public String list(Model model){
        List<EmployeeDO> list = employeeService.getuser();
        model.addAttribute("emps",list);

        System.out.println(list);

//        redisUtils.set("redis_key", "redis_vale");
//        String value = redisUtils.get("redis_key");
//        System.out.println(value);
        return "emp/list";
    }
    @GetMapping("/emps/update")
    public String list1(Model model){
        return "emp/update";
    }
    @RequestMapping(value="/updateEmp",method= RequestMethod.POST)
    public String toUpadteEmp(@RequestBody String userpwd){
        System.out.println((userpwd.split("=",2))[1]);
        employeeService.update((userpwd.split("=",2))[1]);
        return "redirect:emps";
    }

    @GetMapping("/test")
    @ResponseBody
    public void test(HttpServletResponse response) throws Exception {
//       List<EmployeeDO> list = new ArrayList<>();
//       list.add(new EmployeeDO());
        List<EmployeeDO> list = employeeService.getuser();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + "test.xlsx");
        ExcelUtils.downLoadExcel(response,list);
    }


}

